package com.unicom.projt.query;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import com.unicom.projt.Home;
import com.unicom.projt.InputPhoneNumberPage;
import com.unicom.projt.Main;
import com.unicom.projt.R;
import com.unicom.projt.util.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-6
 * Time: 下午3:41
 */
public class QueryCurrentMonthPage extends Activity implements View.OnClickListener {
    private TextView mPhoneNumber;
    private TextView mCurrentDate;
    private TextView mMonthFee;
    private SimpleAdapter listAdapter;
    private ListView mListView;
    private List<HashMap<String, String>> listDate = new ArrayList<HashMap<String, String>>();
    private Handler mHandler;

    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 8;
    private QueryController mController;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.current_month_query);
        ((TextView)this.findViewById(R.id.query_month_number_txt)).setText(UserHolder.phoneNumber);

        mHandler = new Handler();
        mController = new QueryController(this);
        initData();
        queryPhoneAmount();
        this.findViewById(R.id.query_month_back_btn).setOnClickListener(this);
        this.findViewById(R.id.query_month_logout_btn).setOnClickListener(this);
    }

    private void initData() {
        mPhoneNumber = (TextView) this.findViewById(R.id.query_month_phone_number);
        mPhoneNumber.setText(UserHolder.phoneNumber);
        mCurrentDate = (TextView) this.findViewById(R.id.query_month_date);
        mCurrentDate.setText(DateUtils.formatCurrentDate());
        mMonthFee = (TextView) this.findViewById(R.id.query_month_fee);
        mListView = (ListView) this.findViewById(R.id.query_month_list_view);

        listAdapter = new SimpleAdapter(this, listDate, R.layout.month_list_view_item,
                new String[]{"itemTitle", "itemText"},
                new int[]{R.id.item_name, R.id.item_value});
        mListView.setAdapter(listAdapter);

    }

    private void queryPhoneAmount() {
//        initTestData();
        showDialog(DIALOG_PROGRESS);
        mController.queryCM(UserHolder.phoneNumber, DateUtils.formatDateToQueryYearMonth(new Date()));
    }

    //    public void updateCurrentMonthInfo(final String totalFee,List<String> resultList){
    public void updateCurrentMonthInfo(String svcCont) {
        listDate.clear();

        HashMap<String, String> map = new HashMap<String, String>();
        map.put("itemTitle", "预存话费冲销");
        String amount = SimpleXmlParser.getElementsValue(svcCont, "StoredOffsetFee").get(0);
        map.put("itemText", StringUtils.liToYuan(amount) + "元");
        listDate.add(map);

        map = new HashMap<String, String>();
        map.put("itemTitle", "预付话费冲销");
        String amount1 = SimpleXmlParser.getElementsValue(svcCont, "PrepaidOffsetFee").get(0);
        map.put("itemText", StringUtils.liToYuan(amount1) + "元");
        listDate.add(map);

        map = new HashMap<String, String>();
        map.put("itemTitle", "赠送话费冲销");
        String amount2 = SimpleXmlParser.getElementsValue(svcCont, "PresentOffsetFee").get(0);
        map.put("itemText", StringUtils.liToYuan(amount2) + "元");
        listDate.add(map);

        final String totalFee = SimpleXmlParser.getElementsValue(svcCont, "TotalFee").get(0);
        map = new HashMap<String, String>();
        map.put("itemTitle", "总费用");
        map.put("itemText", StringUtils.liToYuan(totalFee) + "元");
        listDate.add(map);

        List<String> resultList = SimpleXmlParser.getElementsValue(svcCont, "AccList");
        for (String record : resultList) {
            map = new HashMap<String, String>();
            map.put("itemTitle","--"+ SimpleXmlParser.getElementsValue(record, "AccType").get(0));
            map.put("itemText", "");
            listDate.add(map);
            List<String> accList = SimpleXmlParser.getElementsValue(record, "DetailList");
            for (String acc : accList) {
                map = new HashMap<String, String>();
                map.put("itemTitle", "----"+SimpleXmlParser.getElementsValue(acc, "AccName").get(0));
                String tempAmount = SimpleXmlParser.getElementsValue(acc, "AccFee").get(0);
                map.put("itemText", StringUtils.liToYuan(tempAmount) + "元");
                listDate.add(map);
            }
        }
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                mMonthFee.setText(StringUtils.liToYuan(totalFee) + "元");
            }
        });

        listAdapter.notifyDataSetChanged();
    }

    private void initTestData(){
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("itemTitle", "基本套餐费");
        map.put("itemText", "220.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "本地通话费");
        map.put("itemText", "120.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "增值业务费");
        map.put("itemText", "20.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "   3G上网费");
        map.put("itemText", "10.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "视频通话费");
        map.put("itemText", "10.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "视频通话费");
        map.put("itemText", "10.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "视频通话费");
        map.put("itemText", "10.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "视频通话费");
        map.put("itemText", "10.00元");
        listDate.add(map);   map = new HashMap<String, String>();
        map.put("itemTitle", "视频通话费");
        map.put("itemText", "10.00元");
        listDate.add(map);
        map = new HashMap<String, String>();
        map.put("itemTitle", "视频通话费");
        map.put("itemText", "10.00元");
        listDate.add(map);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.query_month_back_btn:
                PageJumpUtils.jump(this, Home.class);
                this.finish();
                break;
            case R.id.query_month_logout_btn:
                AlertDialogUtils.showYesNoDiaLog(this, "是否注销？", new AlertDialogUtils.Executor() {
                    @Override
                    public void execute() {
                        UserHolder.clean();
                        doLogout();
                    }
                });
                break;
        }
    }

    private void doLogout(){
        Intent intent = new Intent(this, InputPhoneNumberPage.class);
        this.startActivity(intent);
        this.finish();
    }
    @Override
    public void onBackPressed() {
        PageJumpUtils.jump(this, Home.class);
        this.finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                mDialog.setCancelable(false);
                mDialog.setOnKeyListener(new DialogInterface.OnKeyListener(){
                    public boolean onKey(DialogInterface dialog, int keyCode,KeyEvent event) {
                        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
                            return true;
                        }
                        return false;
                    }
                });
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
